﻿"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var zlib = require('zlib');
var input = "E:\\BeautyIsNow\\data";
var output = "E:\\BeautyIsNow\\data-msg";
var versionInput = "";
var cfgout = "";
var args = process.argv.splice(2);
if (args.length > 0) {
    input = args[0];
}
if (args.length > 1) {
    output = args[1];
}
if (args.length > 2) {
    versionInput = args[2];
}
if (args.length > 3) {
    cfgout = args[3];
}
var tables = readTableCsv();
var all = [];
var allByte = 2 * 1024 * 1024;
var sum = 0;
var page = 0;
for (var i = 0; i < tables.length; i++) {
    var table = tables[i][0];
    var nt = Number(tables[i][1]);
    var key = tables[i][2].split("#");
    var curName = tables[i][3];
    var ienum = Number(tables[i][4]);
    var packgroup = Number(tables[i][5]);
    var json = void 0;
    json = {};
    var data = null;
    if (!data) {
        console.log("解析input:", input + "/" + table + ".csv");
        data = fs.readFileSync(input + "/" + table + ".csv", 'utf8');
    }
    var buf1 = new Buffer(data);
    console.log(table + ":" + getPrintSize(buf1.length));
    sum += buf1.length;
    data = data.replace(/\r/gi, "");
    var lines = data.split("\n");
    var keys = String(lines[3]).split(",");
    var types = String(lines[2]).split(",");
    for (var x = 4; x < lines.length; x++) {
        if (!lines[x])
            continue;
        if (lines[x].length == 0)
            continue;
        var fields = lines[x].split(",");
        if (table == "cfg_box" && Number(fields[2]) == 0)
            continue;
        var a = {};
        // let a = ienum ? [] : {};
        for (var j = 0; j < fields.length; j++) {
            if (types[j] == "")
                continue;
            if (fields[j].length == 0)
                continue;
            var value = types[j] == "string" ? fields[j] : Number(fields[j]);
            if (ienum) {
                a["" + j] = value;
            }
            else {
                a[keys[j]] = value;
            }
        }
        if (nt == 0) { //是对象
            if (key.length == 1 && key[0] == "") {
                key[0] = "id";
            }
            var myKey = [];
            for (var qq = 0; qq < key.length; qq++) {
                if (ienum) {
                    myKey.push("" + keys.indexOf(key[qq]));
                }
                else
                    myKey.push(key[qq]);
            }
            var temp = json;
            while (myKey.length) {
                var tempKey = myKey.shift();
                if (myKey.length == 0) {
                    temp[a[tempKey]] = a;
                }
                else {
                    if (!temp[a[tempKey]])
                        temp[a[tempKey]] = {};
                    temp = temp[a[tempKey]];
                }
            }
        }
        else if (nt == 1) { //是数组
            if (key.length == 1 && key[0] == "") {
                if (json instanceof Array) {
                    json.push(a);
                }
                else {
                    if (json)
                        json = [];
                    json.push(a);
                }
            }
            else {
                var myKey = [];
                for (var qq = 0; qq < key.length; qq++) {
                    if (ienum) {
                        myKey.push("" + keys.indexOf(key[qq]));
                    }
                    else
                        myKey.push(key[qq]);
                }
                var temp = json;
                while (myKey.length) {
                    var tempKey = myKey.shift();
                    if (!temp[a[tempKey]]) {
                        if (myKey.length == 0)
                            temp[a[tempKey]] = [];
                        else
                            temp[a[tempKey]] = {};
                    }
                    if (myKey.length == 0) {
                        temp[a[tempKey]].push(a);
                    }
                    else {
                        temp = temp[a[tempKey]];
                    }
                }
            }
        }
    }
    if (sum >= allByte) {
        sum = 0;
        page++;
    }
    if (!all[page])
        all[page] = {};
    all[page][curName] = json;
    console.log("tables[i][3]", tables[i][3]);
    let curJson = {};
    curJson[curName] = json;
    // fs.writeFileSync(output + "/" + cfgout + "/" + versionInput + curName + ".json", JSON.stringify(curJson));
}
next();
for (var ttt = 0; ttt < all.length; ttt++) {
    fs.writeFileSync(output + "/" + cfgout + "/" +  "config" + ttt + ".json", JSON.stringify(all[ttt]));
}
fs.writeFileSync(output + "/" + cfgout + "/" +  "configNum.json", all.length+"");
console.log("input", input);
console.log("output", output);
console.log("input", input);
console.log("output", output);
console.log("versionInput", versionInput);
//特殊处理表
function next() {
    for (var i = 0; i < 6; i++) {
        for (var tableName in all[i]) {
            next1(tableName, all[i][tableName], all[i]);
        }
    }
}
function next1(tableName, tableValue, json) {
    var eachItem;
    switch (tableName) { //多级key 的 在这里特殊处理下
        case "modelInfo":
            var modelScale = true;
            var POS_SCALE_WING = 0.82;
            var POS_SCALE_MONSTER = 0.83333;
            var POS_SCALE_EFFECT = 0.75;
            for (var key in tableValue) {
                eachItem = tableValue[key];
                for (var keyAction in eachItem) {
                    var item = eachItem[keyAction];
                    if (modelScale) {
                        if (item.id >= 10000 && item.id < 19000) {
                            item.standx = item.standx * POS_SCALE_EFFECT;
                            item.standy = item.standy * POS_SCALE_EFFECT;
                        }
                        else if (item.id >= 50000 && item.id < 60000) {
                            item.standx = item.standx * POS_SCALE_WING;
                            item.standy = item.standy * POS_SCALE_WING;
                        }
                        else if (item.id >= 4000 && item.id < 5000) {
                            item.standx = item.standx * POS_SCALE_MONSTER;
                            item.standy = item.standy * POS_SCALE_MONSTER;
                        }
                    }
                }
            }
            break;
        case "open_level":
            var open_level = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                var pages = eachItem.page.trim().split("#");
                var type = parseInt(pages[0]);
                var tab = parseInt(pages[1]);
                if (pages.length > 2) {
                    type = type * 100 + tab;
                    tab = parseInt(pages[2]);
                }
                if (!open_level[type])
                    open_level[type] = [];
                open_level[type][tab] = eachItem;
            }
            json[tableName] = open_level;
            break;
        case "event":
            var event_1 = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                var param = eachItem.param.trim().split("_");
                eachItem.params = param;
                event_1[eachItem.id] = eachItem;
            }
            json[tableName] = event_1;
            break;
        case "skillEffects":
            var skillEffects = {};
            var bufferEffects = {};
            var monsterEffects = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                if (eachItem.efftype == 0) {
                    if (!(eachItem.skillid in skillEffects)) {
                        skillEffects[eachItem.skillid] = [];
                    }
                    skillEffects[eachItem.skillid].push(eachItem);
                }
                else if (eachItem.efftype == 1) {
                    if (!(eachItem.skillid in bufferEffects)) {
                        bufferEffects[eachItem.skillid] = [];
                    }
                    bufferEffects[eachItem.skillid].push(eachItem);
                }
                else if (eachItem.efftype == 3) {
                    if (!(eachItem.skillid in monsterEffects)) {
                        monsterEffects[eachItem.skillid] = [];
                    }
                    monsterEffects[eachItem.skillid].push(eachItem);
                }
            }
            json["skillEffects"] = skillEffects;
            json["bufferEffects"] = bufferEffects;
            json["monsterEffects"] = monsterEffects;
            break;
        case "lang":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                json[tableName][key] = eachItem.msg;
            }
            break;
        case "deliver":
            var deliver = {};
            var deliverbynpc = {};
            [''];
            for (var key in tableValue) {
                eachItem = tableValue[key];
                deliver[eachItem.id] = eachItem;
                if (deliverbynpc[eachItem.bindNpcId] == null)
                    deliverbynpc[eachItem.bindNpcId] = [];
                deliverbynpc[eachItem.bindNpcId].push(eachItem);
            }
            json.deliver = deliver;
            json.deliverbynpc = deliverbynpc;
            break;
        case "damage":
            var damage = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                var start = eachItem.start;
                var temp1 = start.split("|");
                eachItem.startarr = [];
                for (var i = 0; i < temp1.length; i++) {
                    var temp2 = temp1[i].split("#");
                    eachItem.startarr.push(temp2);
                }
                var stop_1 = eachItem.stop;
                temp1 = stop_1.split("|");
                eachItem.stoparr = [];
                for (var i = 0; i < temp1.length; i++) {
                    var temp2 = temp1[i].split("#");
                    eachItem.stoparr.push(temp2);
                }
                var end = eachItem.end;
                temp1 = end.split("|");
                eachItem.endarr = [];
                for (var i = 0; i < temp1.length; i++) {
                    var temp2 = temp1[i].split("#");
                    eachItem.endarr.push(temp2);
                }
                delete eachItem["start"];
                delete eachItem["end"];
                delete eachItem["stop"];
                damage[eachItem.id] = eachItem;
            }
            json.damage = damage;
            break;
        case "wayGeter":
            var wayGeter = {};
            var wayGeter1 = {};
            [''];
            for (var key in tableValue) {
                eachItem = tableValue[key];
                wayGeter[eachItem.id] = eachItem;
                wayGeter1[eachItem.item] = eachItem;
            }
            json.wayGeter = wayGeter;
            json.wayGeter1 = wayGeter1;
            break;
        case "equipOrange":
            var equipOrange = {};
            var equipOrangeDicByItemId = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                if (!equipOrange[eachItem.career]) {
                    equipOrange[eachItem.career] = {};
                }
                if (!equipOrange[eachItem.career][eachItem.equipSlot]) {
                    equipOrange[eachItem.career][eachItem.equipSlot] = {};
                }
                equipOrange[eachItem.career][eachItem.equipSlot][(eachItem.reinlv ? eachItem.reinlv : 0) * 10000 + (eachItem.uselv ? eachItem.uselv : 0)] = eachItem;
                equipOrangeDicByItemId[eachItem.id] = eachItem;
            }
            json.equipOrange = equipOrange;
            json.equipOrangeDicByItemId = equipOrangeDicByItemId;
            break;
        case "dicLimitTaskDic":
            var dicLimitTaskDic = {};
            var dicLimitTaskTypeDic = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                if (!dicLimitTaskTypeDic[eachItem.type])
                    dicLimitTaskTypeDic[eachItem.type] = [];
                dicLimitTaskTypeDic[eachItem.type].push(eachItem);
                dicLimitTaskDic[eachItem.taskId] = eachItem;
                eachItem.value = 0;
                eachItem.state = -1;
                eachItem.endTime = -1;
            }
            json.dicLimitTaskDic = dicLimitTaskDic;
            json.dicLimitTaskTypeDic = dicLimitTaskTypeDic;
            break;
        case "wareHouseCfg":
            var wareHouseCfg = {};
            var wareHouseKeyTypeCfg = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                wareHouseCfg[eachItem.item] = eachItem;
                if (!wareHouseKeyTypeCfg[eachItem.type]) {
                    wareHouseKeyTypeCfg[eachItem.type] = [];
                }
                wareHouseKeyTypeCfg[eachItem.type].push(eachItem);
            }
            json.wareHouseCfg = wareHouseCfg;
            json.wareHouseKeyTypeCfg = wareHouseKeyTypeCfg;
            break;
        case "drugIdDic":
            var drugClsDic = {};
            var drugIdDic = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                drugIdDic[eachItem.id] = eachItem;
                if (!drugClsDic[eachItem.cls]) {
                    drugClsDic[eachItem.cls] = {};
                }
                drugClsDic[eachItem.cls][eachItem.lv] = eachItem;
            }
            json.drugIdDic = drugIdDic;
            json.drugClsDic = drugClsDic;
            break;
        case "lingshou":
            var lingshou = {};
            var lingshou2 = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                if (!lingshou[eachItem.type]) {
                    lingshou[eachItem.type] = {};
                }
                if (!lingshou[eachItem.type][eachItem.level]) {
                    lingshou[eachItem.type][eachItem.level] = {};
                }
                lingshou[eachItem.type][eachItem.level][eachItem.xlevel] = eachItem;
                lingshou2[eachItem.id] = eachItem;
            }
            json.lingshou = lingshou;
            json.lingshou2 = lingshou2;
            break;
        case "tujian":
            json.tujianid = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                for (var key2 in eachItem) {
                    var eachItem2 = eachItem[key2];
                    for (var key3 in eachItem2) {
                        var eachItem3 = eachItem2[key3];
                        json.tujianid[eachItem3.id] = eachItem3;
                    }
                }
            }
            break;
        case "tupo":
            json.tupo_id = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                for (var branch in eachItem) {
                    var eachItem2 = eachItem[branch];
                    for (var level in eachItem2) {
                        var eachItem3 = eachItem2[level];
                        json.tupo_id[eachItem3.id] = eachItem3;
                    }
                }
            }
            break;
        case "lang":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                json[tableName][key] = eachItem.msg;
            }
            break;
        case "officialDaily":
        case "official":
        case "officialDailyReward":
            json[tableName].unshift(undefined);
            if (tableName == "officialDaily") {
                for (var key in tableValue) {
                    eachItem = tableValue[key];
                    if (eachItem) {
                        eachItem.resAble = 1;
                        eachItem.isFirst = false;
                    }
                }
            }
            break;
        case "uiMode":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                var show = eachItem.show.trim().split("#");
                eachItem.show = [];
                for (var i = 0; i < show.length; i++) {
                    eachItem.show.push(parseInt(show[i]));
                }
                var hide = eachItem.hide.trim().split("#");
                eachItem.hide = [];
                for (var i = 0; i < hide.length; i++) {
                    eachItem.hide.push(parseInt(hide[i]));
                }
                if (eachItem.angles) {
                    var angles = eachItem.angles.trim().split("#");
                    eachItem.angles = [];
                    for (var i = 0; i < angles.length; i++) {
                        eachItem.angles.push(parseInt(angles[i]));
                    }
                }
                if (eachItem.left) {
                    var arr = eachItem.left.trim().split("|");
                    eachItem.left = {};
                    for (var xx of arr) {
                        var arr1 = xx.trim().split("#");
                        eachItem.left[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }
                if (eachItem.right) {
                    var arr = eachItem.right.trim().split("|");
                    eachItem.right = {};
                    for (var yy of arr) {
                        var arr1 = yy.trim().split("#");
                        eachItem.right[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }
                if (eachItem.up) {
                    var arr = eachItem.up.trim().split("|");
                    eachItem.up = {};
                    for (var yy of arr) {
                        var arr1 = yy.trim().split("#");
                        eachItem.up[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }
                if (eachItem.down) {
                    var arr = eachItem.down.trim().split("|");
                    eachItem.down = {};
                    for (var yy of arr) {
                        var arr1 = yy.trim().split("#");
                        eachItem.down[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }
                if (eachItem.hCenter) {
                    var arr = eachItem.hCenter.trim().split("|");
                    eachItem.hCenter = {};
                    for (var yy of arr) {
                        var arr1 = yy.trim().split("#");
                        eachItem.hCenter[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }
                if (eachItem.vCenter) {
                    var arr = eachItem.vCenter.trim().split("|");
                    eachItem.vCenter = {};
                    for (var yy of arr) {
                        var arr1 = yy.trim().split("#");
                        eachItem.vCenter[parseInt(arr1[0])] = parseInt(arr1[1]);
                    }
                }

                json[tableName][key] = eachItem;
            }
            break;
        case "dailyActivityTime":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                eachItem.startTime1 = parseInt(eachItem.startTime) * 60;
                eachItem.overTime1 = parseInt(eachItem.overTime) * 60;
                eachItem.enterTime1 = parseInt(eachItem.enterTime) * 60;
                eachItem.showTime1 = parseInt(eachItem.showTime) * 60;
                eachItem.bstarttime1 = parseInt(eachItem.bstarttime) * 60;
                eachItem.bovertime1 = parseInt(eachItem.bovertime) * 60;
            }
            break;
        case "monstersay":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                if (eachItem.hpPercentMax) {
                    var arr = eachItem.hpPercentMax.trim().split("#");
                    eachItem.hpPercentMax = [];
                    for (var yy of arr) {
                        eachItem.hpPercentMax.push(parseInt(yy));
                    }
                } else {
                    eachItem.hpPercentMax = null;
                }
                if (eachItem.hpPercentMin) {
                    var arr = eachItem.hpPercentMin.trim().split("#");
                    eachItem.hpPercentMin = [];
                    for (var yy of arr) {
                        eachItem.hpPercentMin.push(parseInt(yy));
                    }
                } else {
                    eachItem.hpPercentMin = null;
                }
                if (eachItem.content) {
                    var arr = eachItem.content.trim().split("#");
                    eachItem.content = arr;
                } else {
                    eachItem.content = null;
                }
            }
            break;
        case "monster":
            for (var key in tableValue) {
                eachItem = tableValue[key];
                var moveSpeed = eachItem.moveSpeed;
                if (moveSpeed) {
                    eachItem.moveInterval1 = eachItem.moveInterval / eachItem.moveSpeed;
                    eachItem.moveInterval2 = eachItem.moveInterval1 / 1000 * 60;
                }
            }
            break;
        case "shilianzhilu":
            let shilianzhiluBranch = {};
            for (var key in tableValue) {
                eachItem = tableValue[key];
                let shilianzhiluBranchKey = eachItem.type + "_" + eachItem.chapter + "_" + eachItem.checkpoint;
                if (!shilianzhiluBranch[shilianzhiluBranchKey]) {
                    shilianzhiluBranch[shilianzhiluBranchKey] = [];
                }
                shilianzhiluBranch[shilianzhiluBranchKey][(eachItem.branch - 1)] = eachItem.id;
            }
            for (var key in shilianzhiluBranch) {
                for (let i = 0; i < shilianzhiluBranch[key].length; i++) {
                    let id = shilianzhiluBranch[key][i];
                    tableValue[id].branchIds = shilianzhiluBranch[key];
                }
            }
            break;
    }
}
console.log("完成");
function readTableCsv() {
    var tabledata = fs.readFileSync(input + "/A_tables.csv", 'utf8');
    var lines = tabledata.split("\r\n");
    var arr = [];
    for (var i = 1; i < lines.length; i++) {
        if (lines[i] == "")
            continue;
        arr.push(lines[i].split(","));
    }
    return arr;
}
function getPrintSize(size) {
    //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
    if (size < 1024) {
        return size + "B";
    }
    else {
        size = size / 1024;
    }
    //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
    //因为还没有到达要使用另一个单位的时候
    //接下去以此类推
    if (size < 1024) {
        return size + "KB";
    }
    else {
        size = size / 1024;
    }
    if (size < 1024) {
        //因为如果以MB为单位的话，要保留最后1位小数，
        //因此，把此数乘以100之后再取余
        size = size * 100;
        return (size / 100) + "."
            + (size % 100) + "MB";
    }
    else {
        //否则如果要以GB为单位的，先除于1024再作同样的处理
        size = size * 100 / 1024;
        return (size / 100) + "."
            + size % 100 + "GB";
    }
}
